package com.zwz.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwz.bean.UserInfo;
import com.zwz.dao.UserInfoDao;
import com.zwz.service.UserInfoService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public boolean insert(UserInfo userInfo) {
        int result = userInfoDao.insert(userInfo);
        if (result == 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean deleteById(Integer uid) {
        int result = userInfoDao.deleteById(uid);

        if (result == 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean updateById(UserInfo userInfo) {
        int result = userInfoDao.updateById(userInfo);
        if (result == 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public UserInfo getById(Integer uid) {
        UserInfo userInfo = userInfoDao.selectById(uid);
        return userInfo;
    }

    @Override
    public IPage<UserInfo> getPage(int currentPage, int pageSize, UserInfo userInfo) {
        IPage<UserInfo> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(userInfo.getUserName()), UserInfo::getUserName, userInfo.getUserName());
        lqw.like(Strings.isNotEmpty(userInfo.getPhone()), UserInfo::getPhone, userInfo.getPhone());
        lqw.orderByDesc(UserInfo::getUid);
        userInfoDao.selectPage(page, lqw);
        return page;
    }

    @Override
    public int countAll() {
        int result = userInfoDao.countAll();
        return result;
    }

    @Override
    public boolean userUpdateInfo(UserInfo userInfo) {
        UpdateWrapper<UserInfo> userInfoUpdateWrapper = new UpdateWrapper<>();
        userInfoUpdateWrapper.eq("id",userInfo.getId());
        return userInfoDao.update(userInfo,userInfoUpdateWrapper) == 1;
    }

    @Override
    public UserInfo userShow(Integer id) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return userInfoDao.selectOne(queryWrapper);
    }
}
